package mht.practice.ListNode;

import java.util.ArrayList;
import java.util.Stack;

/**
 * Created by MHT on 2017/9/15.
 */
public class printListFromTailToHead {

    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {

        ArrayList list = new ArrayList();

        Stack<Integer> stack = new Stack<Integer>();


        while (listNode != null){
            stack.add(listNode.val);
            listNode = listNode.next;
        }

        while (!stack.isEmpty()){
            list.add(stack.pop());
        }

        return list;

    }


    /**
     * 从尾到头打印链表，使用递归
     */
    public static void reversePrintListRec(ListNode head) {
        if (head == null) {
            return;
        } else {
            reversePrintListRec(head.next);
            System.out.print(head.val + " ");
        }
    }
}

